<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://unpkg.com/axios@0.24.0/dist/axios.min.js"></script>
</head>
<body>
    <input id="fileInput" type="file" multiple/>
    <script>
        const fileInput = document.querySelector('#fileInput');

        //单文件上传
        async function formData() {
            const data = new FormData();
            data.set('name','newking');
            data.set('age', 25);
            data.set('aaa', fileInput.files[0]);

            const res = await axios.post('http://localhost:3000/aaa', data);
            console.log(res);
        }

        //多文件上传
        async function formData2() {
            const data = new FormData();
            data.set('name','newking');
            data.set('age', 25);
            [...fileInput.files].forEach(item => {
                data.append('bbb', item)
            })

            const res = await axios.post('http://localhost:3000/bbb', data, {
                headers: { 'content-type': 'multipart/form-data' }
            });
            console.log(res);
        }

        //多文件字段
        async function formData3() {
            const data = new FormData();
            data.set('name','newking');
            data.set('age', 25);
            data.append('aaa', fileInput.files[0]);
            data.append('aaa', fileInput.files[1]);
            data.append('bbb', fileInput.files[2]);
            data.append('bbb', fileInput.files[3]);

            const res = await axios.post('http://localhost:3000/ccc', data);
            console.log(res);
        }

        //任意多字段文件上传
        async function formData4() {
            const data = new FormData();
            data.set('name','光');
            data.set('age', 20);
            data.set('aaa', fileInput.files[0]);
            data.set('bbb', fileInput.files[1]);
            data.set('ccc', fileInput.files[2]);
            data.set('ddd', fileInput.files[3]);

            const res = await axios.post('http://localhost:3000/ddd', data);
            console.log(res);
        }

        fileInput.onchange = formData;
    </script>
</body>
</html>
